package com.example.demo.service;

import com.baidu.bas.core.service.IEruptDataService;
import com.baidu.bas.core.view.EruptModel;
import com.deepzero.mmma.api.channel.wechat.WechatNews;
import com.deepzero.mmma.api.vo.Variable;
import com.deepzero.mmma.channel.IRequest;
import com.deepzero.mmma.channel.ISender;
import com.deepzero.mmma.channel.SendResult;
import com.deepzero.mmma.channel.SenderFactory;
import com.deepzero.mmma.channel.mp_kefu_reply.manager.WxMpUserManager;
import com.deepzero.mmma.channel.wx_account.WxMpAccount;
import com.example.demo.model.Account;
import com.example.demo.model.SmsTemplate;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author liuzhenchuan@foxmail.com
 * @date 2021/1/6
 */
@Service
@Slf4j
public class TryService {

    @Resource
    private IEruptDataService iEruptDataService;

    @Resource
    private JdbcTemplate jdbcTemplate;

    /**
     * 获取accountId下的用户.
     * 只适合少量演示账号.
     * @param accountId
     * @return
     */
    public Optional<List<WxMpUser>> wxUsers(Long accountId){
        com.deepzero.mmma.channel.Account config = getAccountConfig(accountId);
        WxMpUserManager manager = new WxMpUserManager((WxMpAccount)config);
        List<String> openIds = manager.fetchAll();
        return manager.fetchUserInfo(openIds);
    }

    public Account getAccount(Long accountId){
        return (Account) iEruptDataService.findDataById(new EruptModel(Account.class),accountId);
    }

    public SendResult sendWeixinReply(Long accountId, WechatNews news, String toUser, List<Variable> vars){
        com.deepzero.mmma.channel.Account config = getAccountConfig(accountId);

        com.deepzero.mmma.channel.mp_kefu_reply.Request request = new com.deepzero.mmma.channel.mp_kefu_reply.Request();
        request.setToUser(toUser);
        request.setData(news);
        if(!CollectionUtils.isEmpty(vars)){
            request.setParams(Variable.toParams(vars));
        }
        ISender<IRequest> sender = SenderFactory.getSender(request,config);
        return sender.send(request);
    }

    private com.deepzero.mmma.channel.Account getAccountConfig(Long accountId) {
        Account account = (Account) iEruptDataService.findDataById(new EruptModel(Account.class), accountId);
        return account.getConfig();
    }

    public SendResult sendWeixinTemplate(Long accountId,String templateId,String toUser,List<Variable> vars){
        com.deepzero.mmma.channel.Account config = getAccountConfig(accountId);

        com.deepzero.mmma.channel.mp_template.Request request = new com.deepzero.mmma.channel.mp_template.Request();
        request.setTemplateId(templateId);
        request.setToUser(toUser);
        request.setVars(vars);
        ISender<IRequest> sender = SenderFactory.getSender(request,config);
        return sender.send(request);
    }

    public SendResult sendSms(Long accountId, Long smsTemplateId, String toUser, List<Variable> vars){
        com.deepzero.mmma.channel.Account config = getAccountConfig(accountId);

        com.deepzero.mmma.channel.sms.Request request = new com.deepzero.mmma.channel.sms.Request();
        SmsTemplate smsTemplate = (SmsTemplate) iEruptDataService.findDataById(new EruptModel(SmsTemplate.class),smsTemplateId);
        request.setSignName(smsTemplate.getSignature());
        request.setToUser(toUser);
        request.setTemplateId(smsTemplate.getOuterId());
        if(CollectionUtils.isNotEmpty(vars)){
            request.setParams(Variable.toParams(vars));
        }

        ISender<IRequest> sender = SenderFactory.getSender(request,config);
        return sender.send(request);
    }

}
